package io.github.talelin.latticy.controller.api;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.github.talelin.core.annotation.PermissionModule;
import io.github.talelin.latticy.common.mybatis.Page;
import io.github.talelin.latticy.common.util.PageUtil;
import io.github.talelin.latticy.dto.article.CreateOrUpdateArticleDTO;
import io.github.talelin.latticy.dto.article.ExpressDTO;
import io.github.talelin.latticy.model.ArticleDO;
import io.github.talelin.latticy.service.ArticleService;
import io.github.talelin.latticy.vo.ArticleVO;
import io.github.talelin.latticy.vo.CreatedVO;
import io.github.talelin.latticy.vo.PageResponseVO;
import io.github.talelin.latticy.vo.UnifyResponseVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.Positive;
import java.util.Map;

/**
 * @author generator@lms
 * @since 2021-04-13
 */
@RestController
@RequestMapping("/api/article")
@Validated
@PermissionModule("文章")
public class ArticleApiController {

    @Autowired
    private ArticleService articleService;

    //获取文章分页
    @GetMapping("/page")
    public PageResponseVO<ArticleDO> page(
            @RequestParam(name = "count", required = false, defaultValue = "10")
            @Min(value = 1, message = "{page.count.min}")
            @Max(value = 30, message = "{page.count.max}") Integer count,
            @RequestParam(name = "page", required = false, defaultValue = "0")
            @Min(value = 0, message = "{page.number.min}") Integer page,
            @RequestParam Integer type
    ) {
        Page<ArticleDO> pager = new Page<>(page, count);
        IPage<ArticleDO> paging = articleService.getBaseMapper().selectPage(pager, new QueryWrapper<ArticleDO>()
                .eq("type", type)
                .eq("audit", 1)
                .orderByDesc("create_time"));
        return PageUtil.build(paging);
    }

    //获取文章及其下面的评论
    @GetMapping("/{id}")
    public ArticleVO get(@PathVariable(value = "id") @Positive(message = "{id.positive}") Integer id) {
        return articleService.getArticle(id);
    }

    //点赞文章
    @PutMapping("/addLike/{id}")
    public UnifyResponseVO<Map<String, Integer>> addLike(
            @PathVariable(value = "id") @Positive(message = "{id.positive}") Integer id,
            @RequestParam Integer uid) {
        Map<String, Integer> map = articleService.addLike(id, uid);
        return new UnifyResponseVO<>(map);
    }

    //收藏文章
    @PutMapping("/collect/{id}")
    public UnifyResponseVO<Map<String, Integer>> addCollect(
            @PathVariable(value = "id") @Positive(message = "{id.positive}") Integer id,
            @RequestParam Integer uid) {
        Map<String, Integer> map = articleService.addCollect(id, uid);
        return new UnifyResponseVO<>(map);
    }

    //故乡情文章发布
    @PostMapping("/hometown")
    public CreatedVO hometown(@RequestBody @Validated CreateOrUpdateArticleDTO dto) {
        //文章需要审核
        dto.setAudit(0);
        articleService.create(dto);
        return new CreatedVO("文章已经提交，静待管理员审核哟！");
    }

    //社区动态发布
    @PostMapping("/express")
    public CreatedVO express(@RequestBody @Validated ExpressDTO dto) {
        articleService.issueExpress(dto);
        return new CreatedVO("发布成功！");
    }

    //搜索文章
    @GetMapping("/search")
    public PageResponseVO<ArticleDO> searchPage(
            @RequestParam(name = "count", required = false, defaultValue = "10")
            @Min(value = 1, message = "{page.count.min}")
            @Max(value = 30, message = "{page.count.max}") Integer count,
            @RequestParam(name = "page", required = false, defaultValue = "0")
            @Min(value = 0, message = "{page.number.min}") Integer page,
            @RequestParam Integer type,
            @RequestParam String key
    ) {
        Page<ArticleDO> pager = new Page<>(page, count);
        IPage<ArticleDO> paging = articleService.getBaseMapper().selectPage(pager, new QueryWrapper<ArticleDO>()
                .eq("type", type)
                .eq("audit", 1)
                .like("title",key));
        return PageUtil.build(paging);
    }
}
